<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div class="area-header">
        <span class="area-header-title">{{ $t('新闻信息') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('新闻标题')" prop="newTitle">
          <el-input
            v-model="queryParams.newTitle"
            :placeholder="$t('新闻标题')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('新闻来源')" prop="newSource">
          <el-input
            v-model="queryParams.newSource"
            :placeholder="$t('新闻来源')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['company:news:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('新闻信息') }}</span>
      </div>
      <!-- <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('新闻分类')" prop="newTitle">
          <el-select v-model="queryParams.emotion" placeholder="请选择" clearable @change="getList">
            <el-option
              v-for="item in emotionOption"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form> -->
      <div v-if="sourceType !== 'bussRisk'" class="right">
        <el-button
          v-hasPermi="['company:news:edit']"
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>
    <div>
      <el-radio-group v-model="classify" class="radio-view-mini" style="white-space: nowrap;" @change="getListByEmotion(classify)">
        <el-radio-button :label="-2">{{ $t('全部') }}</el-radio-button>
        <el-radio-button v-for="(v, index) in emotionOption" :key="index" :label="v.dictValue">{{ v.dictLabel }}({{ v.count }})</el-radio-button>
      </el-radio-group>
      <ul v-if="companyNewsList.length > 0" class="news">
        <li v-for="row in companyNewsList" :key="row.id">
          <div class="newTitle">
            <el-tag v-if="row.emotion=='-1'" effect="plain" type="danger" style="border-color: rgba(219, 90, 82, 1);color: rgba(219, 90, 82, 1);">负面</el-tag>
            <el-tag v-else-if="row.emotion=='1'" effect="plain" style="border-color: rgba(67, 78, 184, 1);color: rgba(67, 78, 184, 1);">正面</el-tag>
            <el-tag v-else-if="row.emotion=='0'" effect="plain" type="info" style="border-color: rgba(153, 153, 153, 1);color: rgba(153, 153, 153, 1);">中性</el-tag>
            <h3 class="link-type" @click="openNews(row)">{{ row.newTitle }}</h3>
            <i>{{ parseTime(row.newTime) }}</i>
          </div>
          <div class="pre">{{ row.abstracts }}</div>
          <div class="newSource">
            <span>来源: {{ row.newSource }}</span>
            <span>相关: <i v-for=" (item,index) in row.companyNewsTagsList" :key="index" class="link-type">{{ item.tags }}</i></span>
          </div>
        </li>
      </ul>
      <el-empty v-else description="暂无数据" />

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改新闻信息对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <companyNewsForm ref="companyNewsForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompanyNews,
  delCompanyNews,
  exportCompanyNews,
  getEmotionInfoList
} from '@/api/company/news/companyNews'
import companyNewsForm from './companyNewsForm.vue'
export default {
  components: {
    companyNewsForm
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 新闻信息表格数据
          companyNewsList: [],
          // 弹出层标题
          title: '新闻信息',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              companyId: undefined,
              newTitle: undefined,
              newSource: undefined,
              newTime: undefined,
              emotion: undefined,
              pageNum: 1,
              pageSize: 5
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          color: ['', 'success', 'info', 'warning', 'danger'],
          emotionOption: [],
          classify: -2
      }
  },
  created() {
    getEmotionInfoList(this.$route.query.companyId).then((response) => {
      this.emotionOption = response.data || []
    })
    this.getList()
  },
  methods: {
    /** 查询新闻信息列表 */
    getList() {
        this.loading = true
        this.queryParams.companyId = this.$route.query.companyId
        listCompanyNews(this.queryParams).then(response => {
            this.companyNewsList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    getListByEmotion(emotion) {
      if (emotion === -2) {
        this.queryParams.emotion = undefined
      } else {
        this.queryParams.emotion = emotion
      }
      this.getList()
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            newTitle: undefined,
            newUrl: undefined,
            newSource: undefined,
            newTime: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.companyNewsForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyNewsForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delCompanyNews(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        var queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportCompanyNews(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    openNews(row) {
      if (row.newUrl.indexOf('http://') !== -1) {
        window.open(row.newUrl)
      } else if (row.newUrl.indexOf('https://') !== -1) {
        window.open(row.newUrl)
      } else {
        window.open('http://' + row.newUrl)
      }
    },
    randumColor() {
        const a = Math.random()
        const b = a * 5
        // 向下取整
        const c = Math.floor(b)// 向下取整
        return this.color[c]
    }
  }
}
</script>
<style lang="scss" scoped>
 .pre {
    white-space: pre-line !important;
    word-break: break-all;
    word-wrap: break-word;
    text-align:left;
    line-height: 18px;

}
.rim{
  margin: 0px 4px;
}
/deep/.radio-view-mini{
  .el-radio-button__inner {
    padding: 4px 13px !important;
    border-radius: 15px !important;
    line-height: 20px;
    background: #f3f6fb;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.54);
    box-shadow: none !important;
    margin-right: 10px;
    font-size: 13px !important;
    border-color: #f3f6fb!important;
  }
  .el-radio-button:first-child .el-radio-button__inner {
    border-color: transparent!important;
  }
  .el-radio-button__orig-radio:checked+.el-radio-button__inner {
    background: #fff!important;
    border-color: rgba(67, 78, 184, 1)!important;
    font-weight: 500!important;
    color: rgba(67, 78, 184, 1)!important;
    box-shadow: none!important;
  }
}
.news{
  li{
    border: 1px solid rgba(235, 238, 248, 1);
    padding: 10px;
    margin-top: 8px;
    border-radius: 6px;
    i{
      font-style: normal;
    }
  }
  .newTitle{
    display: flex;
    align-items: center;
    h3{
      margin: 0 10px;
      flex: 1;
      white-space: nowrap;
      color: rgba(51, 51, 51, 1)!important;
      font-size: 15px;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    i{
      color: rgba(136, 136, 136, 1);
      font-style: normal;
      font-size: 13px;
    }
    .el-tag{
      height: 22px;
      line-height: 20px;
      padding: 0 7px;
      border-radius: 10px;
    }
  }
  .pre{
    margin: 8px 0;
    color: rgba(0, 0, 0, .8);
    font-size: 13px;
  }
  .newSource{
    color: rgba(136, 136, 136, 1);
    font-size: 13px;
    span{
      margin-right: 10px;
      i{
        margin-right: 7px;
      }
    }
  }
}
</style>
